
% similar idea to nested BootstrapNVCsample_v1_0 but set up for high res
% widefield results (using the table output by
% extractWidefieldTrajParams_v1_0)
%
% will want to remove unassigned vessels prior to running

function [idx,mouse_uid,session_uid,trial_uid,vessel_uid] = nestedBootstrapNVCsample_widefield_v1_0(pooledData,samplesPerRepeat,numRepeats)

%% set up nested index cell array
identifierTable = table(pooledData.mouseID,pooledData.sessionID,pooledData.trialnum,pooledData.vesselID);
identifierTable.Properties.VariableNames = ["mouseID","sessionID","trialnum","vesselID"];
[~,~,uniqueIDs] = unique(identifierTable,'rows','stable');
identifierTable.uniqueIDs = uniqueIDs;
[mouse_uid,~,mid_idx] = unique(identifierTable.mouseID);
nummice = length(mouse_uid);
selectionIdx = cell(nummice,1);
maxVesselPoints = 0;
for n = 1:nummice
    cur = identifierTable(mid_idx == n,:);
    [session_uid,~,b] = unique(cur.sessionID);
    numsessions = length(session_uid);
    selectionIdx{n} = cell(numsessions,1);
    for n2 = 1:numsessions
        cur2 = cur(b==n2,:);
        [trial_uid,~,d] = unique(cur2.trialnum);
        numtrials = length(trial_uid);
        selectionIdx{n}{n2} = cell(numtrials,1);
        for n3 = 1:numtrials
            cur3 = cur2(d==n3,:);
            [vessel_uid,~,~] = unique(cur3.uniqueIDs);
            numvessels = length(vessel_uid);
            selectionIdx{n}{n2}{n3} = cell(numvessels,1);
            for n4 = 1:numvessels
                temp = ismember(identifierTable.uniqueIDs,vessel_uid(n4));
                selectionIdx{n}{n2}{n3}{n4} = find(temp);
                maxVesselPoints = max([maxVesselPoints,sum(temp)]);
            end
        end
    end
end

%sample mice:
idx = cell(5,1);
for n = 1:(length(idx)-1)
    idx{n} = zeros(samplesPerRepeat,numRepeats);
end
idx{1} = randi(length(selectionIdx),samplesPerRepeat,numRepeats);       %randomly choose mice
[u1,~,u1idx] = unique(idx{1});
for n = 1:length(u1)
    cur = selectionIdx{u1(n)};
    temp = randi(length(cur),sum(u1idx == n),1);            %for a given mouse, randomly choose the sessions
    i1 = find(u1idx == n);
    idx{2}(i1) = temp;
    [u2,~,u2idx] = unique(temp);
    for n2 = 1:length(u2)
        cur = selectionIdx{u1(n)}{u2(n2)};
        temp2 = randi(length(cur),sum(u2idx == n2),1);      %for a given mouse-session, randomly choose trials
        i2 = find(u2idx == n2);
        idx{3}(i1(i2)) = temp2;
        [u3,~,u3idx] = unique(temp2);
        for n3 = 1:length(u3)
            cur = selectionIdx{u1(n)}{u2(n2)}{u3(n3)};
            temp3 = randi(length(cur),sum(u3idx == n3),1);      %for a given mouse-session, randomly choose vessels
            i3 = find(u3idx == n3);
            idx{4}(i1(i2(i3))) = temp3;
        end
    end
end
for n = 1:numRepeats
    cur = [];
    for n2 = 1:samplesPerRepeat
        cur = [cur;selectionIdx{idx{1}(n2,n)}{idx{2}(n2,n)}{idx{3}(n2,n)}{idx{4}(n2,n)}];
    end
    idx{5} = nancat(idx{5},cur,2);
end



